<?php
namespace app\web\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Complain as Complain_mod;

class Complain extends Base
{
    public function Complain()
    {
global $_G,$_S,$_C,$_P;
$themeid = intval($_P['themeid']);
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
$themelist = Theme::getAllTheme();
$config =   Theme::getConfig($themeid);
$do = 'complain';
$op = isset($_P['op'])?$_P['op']:'display';
$system = $_G['system'];
$server = findNum(MODULE_NAME);

if ($op=='display'){
    $type = isset($_P['type'])?$_P['type']:'';
    $status = isset($_P['status'])?$_P['status']:0;
    extract(Complain_mod::getDisplay($system,$themeid,$status,$type,true,true));
    //导出
    if(checksubmit('export_submit', true)){
        $count  = $total;
        $pagesize = ceil($count/5000);
        $header = array(
            'id' => '序号',
            'uid' => '投诉人UID',
            'nickname' => '投诉人微信昵称',
            'mobile' => '投诉人手机号',
            'type' => '投诉原因',
            'content' => '投诉内容',
            'order_id' => '定单号',
            'order_username' => '姓名',
            'order_mobile' => '电话',
            'order_fee' => '金额',
            'paytime' => '付款时间',
            'paystatus' => '定单状态',
            'workerinfo' => '所属员工',
            'status' => '状态',
            'created_at' => '投诉时间',
            'updated_at' => '处理时间',
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        //for ($j = 1; $j <= $pagesize; $j++){
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    foreach ($buffer as $index => $row) {
                        if ($row['paytime']){
                            $row['paytime'] = mb_substr($row['paytime'],0,10,'utf-8');
                        }
                        $row['order_id'] = (int)$row['order_id']+9000;
                        switch ($row['paystatus']){
                            case 0:
                                $row['paystatus'] = '未支付';
                                break;
                            case 1:
                                $row['paystatus'] = '已支付';
                                break;
                            case 2:
                                $row['paystatus'] = '已核销';
                                break;
                            case -1:
                                $row['paystatus'] = '已退款';
                                break;
                            case -2:
                                $row['paystatus'] = '虚拟定单';
                                break;
                            case 3:
                                $row['paystatus'] = '线下定单';
                                break;
                            default:
                                $row['paystatus'] = '未知';
                                break;
                        }
                        $row['workerinfo'] = $row['workername'].'-'.$row['teamname'].'-'.$row['companyname'];
                        switch ($row['status']){
                            case 1:
                                $row['status'] = '待处理';
                                break;
                            case 2:
                                $row['status'] = '处理中';
                                break;
                            default:
                                $row['status'] = '已完结';
                                break;
                        }
                        if ($row['status']==2){
                            $row['updated_at'] = $row['worker_info'].'-'.$row['updated_at'];
                        }
                        $row['content'] = str_replace('"','',$row['content']);
                        $row['content'] = str_replace("'",'',$row['content']);
                        if ($row['nickname']!='新用户' && $row['nickname']!='微信用户'){
                            $row['nickname'] = str_replace(',','',$row['nickname']);
                            $row['nickname'] = str_replace('"','',$row['nickname']);
                        }
                        $row['id'] =  $server . $row['id'];

                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        if (isset($data)){
                            $user[] = implode("\t ,", $data) . "\t ,";
                            unset($data);
                        }
                    }
                    $html .= implode("\n", $user)."\n";
                    unset($user);
                }
            }
        //}
        //新版本导出合并
        Util::WxJump($system,$html,'投诉记录',$themeid,1,1);
    }
    $return_clicent = get_defined_vars();
return view('/complain', $return_clicent);
}elseif ($op=='changeEnd'){
//    Complain_mod::chang_end($themeid);
//    show_json(0, ['msg'=>'成功']);
}elseif ($op=='reply'){
    $id = $_P['id'];
    $submit = $_P['submit'];
    $complain = Complain_mod::reply($system,$id,$submit,$_G['username'],true);
    if ($complain['error']==1){
        $url = createWebUrl($do, array('themeid' => $themeid, 'op' => 'reply', 'id' => $id));
        return_tips($complain['msg'], $url, 'info');
    }
    $return_clicent = get_defined_vars();
return view('/complain', $return_clicent);
}elseif($op == 'refund') {
    $opuser = $_G['username'];
    $opuser = str_replace('benben', '管理员', $opuser);
    $order_id = intval($_P['id']);
    $id = intval($_P['complanID']);
    $ret = Complain_mod::refund($order_id,$id,$themeid,$theme,$system,$config,$opuser);
    $url = createWebUrl($do, array('themeid' => $themeid, 'op' => 'reply', 'id' => intval($_P['complanID'])));
    return_tips($ret['msg'], $url, 'success');
}

}
}